﻿#include <iostream>
class MoveObject {
public:
    int x;
    int y;
    MoveObject() {
        std::cout << this << std::endl;
        this->Move();//MoveObject Moving~,调用MoveObject的Move函数
    }
    virtual void Move() {
        std::cout << "MoveObject Moving~\n";
    }
    virtual void AutoMove(int step = 2) {//默认实参以基类为原则，派生类的默认参数无效果，因为编译的时候已经决定了默认参数多少
        std::cout << "auto move" << step << std::endl;
    }
    void test() {
        //this->Move();
    }
    virtual ~MoveObject() {
        std::cout << "~MoveObject" << std::endl;
       // Move();//MoveObject Moving~，因为派生类已经析构，所以Move函数做静态绑定，调用MoveObject的Move函数
    }
};
class MonsterObject:public MoveObject {
public:
    MonsterObject() {
        std::cout << this << std::endl;
        this->Move();//MonsterObject Moving~,调用MoveObject的Move函数
        MoveObject::test();//通过基类MoveObject的test函数调用MoveObject的Move函数，因为MoveObject的Move是虚函数，所以调用MonsterObject的Move函数
        MoveObject::Move();//调用基类MoveObject的Move函数
    }
    virtual void Move() override {
        std::cout << "MonsterObject Moving~\n";
    }
    ~MonsterObject() {
        std::cout << "~MonsterObject" << std::endl;
        //Move();//MonsterObject Moving~
    }
    void AutoMove(int step = 3) override {
        std::cout << "auto move" << step << std::endl;
    }
};
class NPCObject :public MonsterObject {
public:
    virtual void Move() {
        std::cout << "NPCObject Moving~\n";
    }
    ~NPCObject() {
        std::cout << "~NPCObject" << std::endl;
    }
};
int main()
{
    MonsterObject snake;//this指针的地址一样，snake地址就是this指针的地址，只是因为先传入基类MoveObject而已
    snake.test();//MonsterObject Moving~，因为MoveObject类的Move函数是虚函数

    MoveObject* p = new NPCObject();
    p->AutoMove();
    p->Move();//MonsterObject Moving~
    delete p;//~MoveObject，只析构了基类，因为p指针是MoveObject类，且此时因为没有完全释放有内存泄露
    delete p;//析构函数加上virtual后，先~MonsterObject，再~MoveObject
}